package com.wlyuan.open.scanner.domain.quotation;

import com.wlyuan.open.scanner.utils.enums.quotation.QuotationMapping;
import com.wlyuan.open.scanner.utils.mappings.BaseMapping;
import lombok.var;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lijing
 * @description
 */
public class CombineQuotationConvert {

    private static final Convert CONVERT = Mappers.getMapper(Convert.class);

    @Mapper(uses = QuotationMapping.class)
    public interface Convert {
        @Mappings({
                @Mapping(target = "id", source = "orderId"),
                @Mapping(target = "sourceKey", source = "orderId"),
                @Mapping(target = "combineId", source = "orderId"),
                @Mapping(target = "issuer", source = "tenantId"),
                @Mapping(target = "transportFee", expression = "java(com.wlyuan.open.scanner.utils.NumberUtils.add(source.getFullCost(),source.getLessCost()))"),
        })
        @BaseMapping
        CombineQuotation convert(QuotationOrder source);

        @Mappings({
                @Mapping(target = "tenantId", source = "upTenantId"),
        })
        QuotationUp upConvert(QuotationUpDown source);

        @Mappings({
                @Mapping(target = "tenantId", source = "downTenantId"),
        })
        QuotationDown downConvert(QuotationUpDown source);

        @Mappings({
                @Mapping(target = "id", source = "source.id"),
                @Mapping(target = "sourceKey", source = "source.sourceKey"),
                @Mapping(target = "waybillId", source = "source.waybillId"),
                @Mapping(target = "tenantId", source = "source.tenantId"),
                @Mapping(target = "transportMode", source = "source.transportMode"),
                @Mapping(target = "totalWeight", source = "source.totalWeight"),
                @Mapping(target = "totalVolume", source = "source.totalVolume"),
                @Mapping(target = "totalValue", source = "source.totalValue"),
                @Mapping(target = "totalQuantity", source = "source.totalQuantity"),
                @Mapping(target = "createAt", source = "source.createAt"),

                @Mapping(target = "transportFee", source = "match.transportFee"),
                @Mapping(target = "pickCost", source = "match.pickFee"),
                @Mapping(target = "deliveryCost", source = "match.deliverFee"),
                @Mapping(target = "onLoadCost", source = "match.loadFee"),
                @Mapping(target = "offLoadCost", source = "match.unloadFee"),
                @Mapping(target = "insuranceCost", source = "match.insureFee"),
                @Mapping(target = "taxCost", source = "match.taxFee"),
                //@Mapping(target = "additionCost", source = "match.additionCost"),
                @Mapping(target = "payment", source = "match.prepayFee"),
                @Mapping(target = "payable", source = "match.arrivePayFee"),
                @Mapping(target = "paysub", source = "match.collectionFee"),
                //@Mapping(target = "totalCost", source = "match.totalCost"),
                @Mapping(target = "minCost", source = "match.minimumFee"),
                @Mapping(target = "fees", source = "source.fees"),

                @Mapping(target = "templateId", source = "match.templateId"),
                @Mapping(target = "refresh", source = "match.refresh"),
                @Mapping(target = "remarks", source = "match.remarks"),
        })
        @BaseMapping
        CombineQuotation convert(CombineQuotation source, QuotationMatch match);

        @Mappings({
                @Mapping(target = "type", source = "type"),
                @Mapping(target = "userId", source = "createBy"),
        })
        QuotationSurcharge convert(QuotationMatch source);
    }

    public static CombineQuotation convert(QuotationOrder source) {
        return CONVERT.convert(source);
    }

    public static QuotationUp upConvert(QuotationUpDown source) {
        var convert = CONVERT.upConvert(source);
        if (source.getCarrierType() == null) {
            return null;
        }
        return convert;
    }

    public static QuotationDown downConvert(QuotationUpDown source) {
        return CONVERT.downConvert(source);
    }

    public static CombineQuotation convert(CombineQuotation source, QuotationMatch match) {
        var quotation = CONVERT.convert(source, match);
        if (CollectionUtils.isEmpty(quotation.getFees())) {
            List<QuotationSurcharge> fees = new ArrayList<>();
            fees.add(convert(match));
            quotation.setFees(fees);
        }
        return quotation;
    }

    public static QuotationSurcharge convert(QuotationMatch match) {
        return CONVERT.convert(match);
    }
}
